home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 5373 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.6 KB

  1. Path: indirect.com!pronet01
  2. From: pronet01@indirect.com (Mark Miller)
  3. Newsgroups: comp.lang.c++
  4. Subject: gnu problem with ternary & references
  5. Date: Sat, 3 Feb 1996 19:12:17 UNDEFINED
  6. Organization: Promark One
  7. Message-ID: <pronet01.26.001ACCB0@indirect.com>
  8. NNTP-Posting-Host: s29.phxslip4.indirect.com
  9. X-Newsreader: Trumpet for Windows [Version 1.0 Rev B final beta #1]
  10.  
  11. Gang,
  12.  
  13. I wanted to share something with you that hopefully will save you quite
  14. a few hours of debugging if you ever run across this scenario...
  15.  
  16. Buried deep into one of my class libraries, I had a method returning
  17. a const reference (for efficiency reasons rather than a copy) to an attribute.
  18. I knew I had a value in the attribute I was trying to get, but was ALWAYS 
  19. getting back an empty string.  I was trying to return a reference to "" rather
  20. than to an static object (that was a string object set to "")..
  21.  
  22. Here's what I saw with Gnu (and can reproduce it everytime) and I'm still
  23. trying to see if this is a compiler bug or a "feature"..
  24.  
  25. If you have a statement like (using C++ Views by Liant):
  26.  
  27. VString & someClass::getString(const int &i)
  28. {
  29.     VString *pString = (VString *)StringCollect.idAt(i);
  30.  
  31.     return pString != (VString *)NULL ? *pString : "";
  32. }
  33.  
  34. Whether pString is NULL or not, you will ALWAYS get back an empty reference.
  35. This is what blew my mind...  
  36.  
  37. IF you replace the:
  38.  
  39.     return pString != (VString *)NULL ? *pString : "";
  40. with:
  41.  
  42.     if (pString != (VString *)NULL)
  43.         return *pString;
  44.     else
  45.         return "";
  46.  
  47. it WILL work...  
  48.  
  49. HOWEVER, the following ALSO WORKS:
  50.  
  51.     static VString emptyRef;
  52.     ...
  53.  
  54.     return pString != (VString *)NULL ? *pString : emptyRef;
  55.  
  56. So, for some reason (I'm hoping GNU was trying to inform me that passing
  57. a reference to "" (by accident) is evil, OR there's a flat out bug in the GNU 
  58. C++ compiler.  Trying the same code with Borland or Microsofts compiler
  59. works with all 3 ways...
  60.  
  61. On the subject of gnu c++ compiler problems includes the other scenario
  62. where the compiler belches when you try and do:
  63.  
  64.     switch (value) {
  65.         case ONE:
  66.             int x;    // Compiler will belch here
  67.             ...
  68.             break;
  69.     }
  70. and requires you to do:
  71.  
  72.     if (value == ONE) {
  73.         int x;        // Compiler will NOT belch here
  74.         ...
  75.     }
  76.  
  77. This may be a dumb question, but in my 3 years of C++ programming, I never
  78. could find the answer to this in any of my 6 books concerning references...
  79.  
  80. I believe that it is evil to return a reference to "" rather than to object x 
  81. (which is set to "")..  If it is NOT evil (if the compiler does something
  82. similar to what 'delete NULL' does), please let me know if you know the
  83. answer..  Please email me directly as I rarely ever logon to the net...
  84.  
  85. thanks,
  86. theron kousek
  87. promark 1
  88. phoenix, AZ
  89.     
  90.  
  91.  
  92.